package cn.itycu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itycu.db.DBManager;

/**
 * Servlet implementation class ChukuAction
 */
@WebServlet("/ChukuAction")
public class ChukuAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ChukuAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");
		// 商品出库
		int uid = Integer.parseInt((String) request.getSession().getAttribute("uid"));

		DBManager dbm = new DBManager();
		String hao = dbm.getChe(uid);
		if (hao != null) {
			if (dbm.isDing(hao) != null) {
				java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

				String sql = "update storage set chepai='',bdate='" + format.format(new java.util.Date())
						+ "' where id=" + id;
				System.out.println(sql);
				Statement stat = null;
				Connection conn = null;
				try {
					conn = dbm.getConnection();
					stat = conn.createStatement();
					stat.execute(sql);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					try {
						if (stat != null)
							stat.close();
						if (conn != null)
							conn.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				out.println("<script>alert('出库成功！');window.location.href='chewei/tlist.jsp'</script>");

				
			} else {
				out.println("<script>alert('出库成功！');window.location.href='chewei/tlist.jsp'</script>");
			}

		} else {
			out.println("<script>alert('请添加商品信息！');window.location.href='chewei/tlist.jsp'</script>");
		}

		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
